<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/comm.js"></script>
<script>
	function addColumn(obj) {
		var emptyStrutXml = "<column name='' dataType='d' isPk=''></column>";
		var tableColumnXslDoc = commLoad("createTableRow.xsl");
		var rowXmlDoc = commLoadXML(emptyStrutXml);
		try{
			if (document.implementation && document.implementation.createDocument) {
				xsltProcessor = new XSLTProcessor();
				xsltProcessor.importStylesheet(tableColumnXslDoc);
				resultDocument = xsltProcessor.transformToFragment(rowXmlDoc,document);
			}
			var i = obj.parentNode.parentNode.rowIndex;
			var tbody = obj.parentNode.parentNode.parentNode;
			tbody.appendChild(resultDocument);
		}catch (e) {
			document.write("您的browser不是firefox，请用firefox使用本程序！");
		}
	}

	function removeColumn(obj) {
		//alert(1);
		var tbody = obj.parentNode.parentNode.parentNode;
		var tbody = obj.parentNode.parentNode.parentNode;
		var row = obj.parentNode.parentNode;
		tbody.removeChild(row);
	}

	function pickUpData() {
		var table = document.getElementById("table");
		var tableName = document.getElementById("tableName");
		var pks = document.getElementById("pk");
		var columnNames = document.getElementsByName("columnName");
		//alert(columnNames.length);
		//alert(table.childNodes);
		var dataTypes = document.getElementsByName("dataType");
		var tableStrutStr = "create table " + tableName.value + " ( ";
		for ( var i = 0; i <= columnNames.length - 1; i++) {
			tableStrutStr = tableStrutStr + columnNames[i].value + " "
					+ dataTypes[i].value;
			if (i != columnNames.length - 1)
				tableStrutStr = tableStrutStr + ",";
		}
		tableStrutStr = tableStrutStr + " ) ";

		alert(tableStrutStr);
		return tableStrutStr;
	}

	function checkPkNumSupported() {
		var pks = document.getElementsByName("pk");
		var pkNum = 0;
		for ( var i = 0; i <= pks.length - 1; i++)
			if (pks[i].checked == true)
				pkNum++;
		if (pkNum > 2) {
			alert("本程序最多支持双主键！");
			return false;
		}
		return true;
	}
	
	function save() {
		if (!checkPkNumSupported())
			return;
		document.getElementById("sqlOut").value = pickUpData();
	}
</script>


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" id="table">
		<tbody>
			<tr>
				<td align="center">表名</td>
				<td colspan="4"><input type="text" id="tableName"></input></td>
			</tr>
			<tr>
				<td align="center">主键</td>
				<td align="center">列名</td>
				<td><input type="button" value="+" onclick="addColumn(this);"></input></td>
				<td><input type="button" value="-"
					onclick="removeColumn(this);"></input></td>
				<td align="center">数据类型</td>
			</tr>
			<tr>
				<td><input type="checkbox" id="pk" name="pk"></input></td>
				<td><input type="text" id="columnName" name="columnName"></input></td>
				<td><input type="button" value="+" onclick="addColumn(this);"></input></td>
				<td><input type="button" value="-"
					onclick="removeColumn(this);"></input></td>
				<td><input type="text" id="dataType" name="dataType"></input></td>
			</tr>
		</tbody>
	</table>
	<div id="tableContent">
		<table>
			<tr>
				<td><input type="button" onclick="pickUpData()" value="确定"></input></td>
				<td><input type="button" onclick="save()" value="保存"></input></td>
			</tr>
		</table>
	</div>
	<div>
		<textarea id="sqlOut"></textarea>
	</div>
</body>
</html>